const {app, BrowserWindow, net, globalShortcut, clipboard, ipcMain, screen} = require('electron')
// const remote = require('@electron/remote/main')
const Store = require('electron-store')

// 初始化
Store.initRenderer()
// remote.initialize()

//记录主窗口id
let mainId = null

function createWindow() {
    const mainWindow = new BrowserWindow({
        show: false,
        width: 800,
        height: 600,
        icon: './static/translate.png',
        frame: false, // 全屏
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            enableRemoteModule: true
        }
    })

    mainWindow.loadFile('./index/index.html')

    mainWindow.on('ready-to-show', () => {
        mainWindow.show()
    })

    mainId = mainWindow.id
    // mainWindow.webContents.openDevTools()
    // remote.enable(mainWindow.webContents)

    // 注册快捷键
    let register = globalShortcut.register('alt + q', (ev) => {
        // 获取鼠标选中的文本
        let text = clipboard.readText('selection');
        let url = 'http://dict.youdao.com/suggest?q=' + text + '&num=1&doctype=json'
        let request = net.request(url);
        request.on('response', (resp) => {
            resp.on('data', (data) => {
                let parse = JSON.parse(data.toString());
                console.log(parse.data.entries)
                const window = BrowserWindow.fromId(mainId)
                window.webContents.send('translate', parse.data.entries)
                let cursorScreenPoint = screen.getCursorScreenPoint();
                window.setPosition(cursorScreenPoint.x, cursorScreenPoint.y + 200)
                window.show()
            })
        })
        request.end()
    });

}

app.whenReady().then(() => {
    createWindow()

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
})

app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit()
})

// ioHook.start(false);
// const eventHandler = function (type) {
//     switch (type) {
//         case 'mouseclick':
//             console.log('mouse is click!')
//             break;
//         case 'mousedown':
//             console.log('mouse is press!')
//             break;
//         case 'mouseup':
//             console.log('mouse is release!')
//             break;
//         case 'mousedrag':
//             console.log('mouse is moving!')
//             break;
//         case 'mousedrag':
//             console.log('mouse is moving!')
//             break;
//         case 'mousewheel':
//             console.log('keybord is rolling!')
//             break;
//         case 'keydown':
//             console.log('keybord is press!')
//             break;
//         default:
//             console.log('move mouse or keyboard try it!')
//             break;
//     }
// }
// ioHook.start(false);
// ioHook.on('mouseclick', () => {
//     eventHandler('mouseclick')
// });
// ioHook.on('mousedown', () => {
//     eventHandler('mousedown')
// });
// ioHook.on('mouseup', () => {
//     eventHandler('mouseup')
// });
// ioHook.on('mousedrag', () => {
//     eventHandler('mousedrag')
// });
// ioHook.on('mousewheel', () => {
//     eventHandler('mousewheel')
// });
// ioHook.on('mouse', () => {
//     eventHandler('mousedrag')
// });
// ioHook.on('keyup', () => {
//     eventHandler('keyup')
// });
// ioHook.on('keydown', () => {
//     eventHandler('keydown')
// });


app.on('before-quit', () => {
    // 卸载iohook监听
    // ioHook.unload();
    // ioHook.stop();
    globalShortcut.unregisterAll()
});
